package com.ljs.springbootinit.mq;

import java.util.Scanner;

public class FanOutProducer {

  private static final String EXCHANGE_NAME = "fanout_exchange";

  public static void main(String[] argv) throws Exception {
      com.rabbitmq.client.ConnectionFactory factory = new com.rabbitmq.client.ConnectionFactory();
      factory.setHost("localhost");
      try (com.rabbitmq.client.Connection connection = factory.newConnection();
           com.rabbitmq.client.Channel channel = connection.createChannel()) {
          //创建一个交换机
          channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
          Scanner scanner = new Scanner(System.in);
          while (scanner.hasNext()) {
              String message = scanner.nextLine();
              channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes("UTF-8"));
              System.out.println(" [x] Sent '" + message + "'");
          }
      }
  }
}